package com.scott.user.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {
	
	
	
	
	
	/**
	 * 关闭查询资源  
	 * @param rs           结果集
	 * @param ps           预编译语句
	 * @param connection   数据库连接
	 */
	public static void closeQueryResource(ResultSet rs,PreparedStatement ps,Connection connection){
		closeResultSet(rs);
		closePreparedStatement(ps);
		closeConnection(connection);
	}
	
	
	
	/**
	 * 关闭结果集
	 * @param rs
	 */
	public static void closeResultSet(ResultSet rs){
		
		if(null != rs){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	/**
	 * 关闭预编译语句
	 * @param ps
	 */
	public static void closePreparedStatement(PreparedStatement ps){
		if(null != ps){
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	/**
	 * 关闭连接
	 * @param connection
	 */
	public static void closeConnection(Connection connection) {
		if(null != connection){
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	/**
	 * 获得数据连接
	 * @return
	 */
	public static Connection getConnection(){
		
		Connection connection = null;
		
		
		try {
			
			Class.forName("com.mysql.jdbc.Driver");
			
			String url="jdbc:mysql://127.0.0.1:3306/scott?useUnicode=true&characterEncoding=UTF-8";
			connection = DriverManager.getConnection(url, "root", "123456");
			
			System.out.println("数据库链接成功..." +connection);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
		
	}
	
	
	

}
